//
//  LocationModel.swift
//  QingLiuSwift
//
//  Created by xyanl on 2024/1/15.
//  Copyright © 2024 考拉🐨. All rights reserved.
//

import Foundation
import CoreLocation
import HandyJSON

/// 位置来源
enum LocationSource {
    /// 定位
    case location
    /// 城市列表选中的
    case selected
}

class LocationModel: HandyJSON {
    
    /// 数据来源
    var source: LocationSource = .location
    
    /// 经纬度
    var coordinate: CLLocationCoordinate2D?
    
    /// 国家 id
    var countryID: Int?
    /// 国家名
    var country: String?
    /// 国家名（英文）
    var countryEn: String?
    
    /// 城市 id
    var cityID: Int?
    /// 城市名
    var city: String?
    /// 城市名（英文）
    var cityEn: String?
    
    /// 用于显示的名字和 id
    var displayName: String?
    var displayId: Int?
    
    /// 默认加拿大-多伦多
    public static var `default`: LocationModel {
        let model = LocationModel()
        model.source = .selected
        model.coordinate = CLLocationCoordinate2D(latitude: 43.6628917, longitude: -79.3978451)
        
        model.countryID = 335
        model.country = "加拿大"
        model.countryEn = "Canada"
        
        model.cityID = 336
        model.city = "多伦多"
        model.cityEn = "Toronto"
        
        model.displayName = "Toronto"
        
        return model
    }
    
    static let shared = LocationModel.default
    
    func clear() {
        coordinate = nil
        countryID = nil
        country = nil
        countryEn = nil
        cityID = nil
        city = nil
        cityEn = nil
    }
    
    class func save(model: LocationModel) {
        
        LocationModel.shared.countryID = model.countryID
        LocationModel.shared.country = model.country
        LocationModel.shared.countryEn = model.countryEn
        
        LocationModel.shared.cityID = model.cityID
        LocationModel.shared.city = model.city
        LocationModel.shared.cityEn = model.cityEn
        
        LocationModel.shared.source = model.source
        LocationModel.shared.coordinate = model.coordinate
        LocationModel.shared.displayName = model.displayName
        LocationModel.shared.displayId = model.displayId
    }
    
    required init() {}
    
    func mapping(mapper: HelpingMapper) {
        mapper <<< 
            [
                self.countryID <-- "countryInfo.id",
                self.country <-- "countryInfo.name",
                self.countryEn <-- "countryInfo.en_name",
                
                self.cityID <-- "cityInfo.id",
                self.city <-- "cityInfo.name",
                self.cityEn <-- "cityInfo.en_name",
            ]
    }
}
